<template>
  <el-dialog
    v-el-drag-dialog
    title="退款"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="160px">
    <el-form-item label="订单金额" prop="totalMoney">
      <el-input type="input" style="width: 160px" v-model="dataForm.totalMoney" placeholder="订单金额" disabled></el-input>
    </el-form-item>
    <el-form-item label="真实退款金额" prop="refundMoney">
      <el-input type="input" style="width: 160px" v-model="dataForm.refundMoney" placeholder="真实退款金额" disabled></el-input>
    </el-form-item>
    <el-form-item label="退款原因" prop="refundReason">
      <el-input type="textarea" v-model="dataForm.refundReason" placeholder="退款原因"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        goodsInfoList: [],
        dataForm: {
          returnId: 0,
          totalMoney: '',
          refundMoney: '',
          refundReason: '',
          orderNo: ''
        },
        dataRule: {
          refundReason: [
            { required: true, message: '退款原因不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    // watch: {
    //   'dataForm.refundMoney': function (val) {
    //     var reg = /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/
    //     if (!reg.test(this.dataForm.refundMoney)) {
    //       this.$message('请规范输入退款的金额！')
    //       return false
    //     }
    //     if (val > this.dataForm.totalMoney) {
    //       this.$message('退款金额不得大于订单总金额！')
    //       return false
    //     }
    //   }
    // },
    methods: {
      init (returnId, orderNo) {
        this.dataForm.returnId = returnId || 0
        this.dataForm.orderNo = orderNo
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.returnId) {
            this.$http({
              url: this.$http.adornUrl(`/returnGoods/getRefundAmount/${this.dataForm.returnId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.refundMoney = data.data.refundMoney
                this.dataForm.totalMoney = data.data.totalMoney
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl('/returnGoods/executionPayment'),
              method: 'post',
              data: this.$http.adornData({
                'returnId': this.dataForm.returnId,
                'orderNo': this.dataForm.orderNo,
                'totalMoney': this.dataForm.totalMoney,
                'refundMoney': this.dataForm.refundMoney,
                'refundReason': this.dataForm.refundReason
              })
            }).then(({data}) => {
              let s = JSON.parse(data.data)
              if (data && s.code === 200) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(s.message)
              }
            })
          }
        })
      }
    }
  }
</script>
